﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class KC_WL_Query : BasePage
    {
        double? PageMoney = 0;//本页总计金额
        static string KCLX = EnumHelper.GetDescriptionByName(typeof(EnumKC_LX), EnumKC_LX.WL);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            this.AspNetPager1.PageSize = GridView1.PageSize;
            BindWLLX(this.ddlWLLX, "请选择");
            BindGYS(this.ddlGYSID, "请选择");
            using (KCBLL kBLL = new KCBLL())
            {
                double? totalMoney = kBLL.GetList(t => t.WPLX == KCLX).Sum(t => t.SL * t.PJDJ);
                liTotalMoney.Text = totalMoney.Value.ToString("C");
            }
        }

        private void BindData()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            dc.Add("WPLX", KCLX);
            if (!string.IsNullOrEmpty(tbBH.Text))
                dc.Add("BH", tbBH.Text.Trim());
            if (!string.IsNullOrEmpty(tbMC.Text))
                dc.Add("MC", tbMC.Text.Trim());
            if (!string.IsNullOrEmpty(ddlWLLX.SelectedValue))
                dc.Add("WLLX", ddlWLLX.SelectedValue);
            if (!string.IsNullOrEmpty(ddlGYSID.SelectedValue))
                dc.Add("GYS", ddlGYSID.SelectedValue);
            using (KCBLL kBLL = new KCBLL())
            {
                int RecordCount = 0;
                List<WLKCModel> itemLists = kBLL.GetWLKC(dc, true, "BH asc", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
            liPageMoney.Text = PageMoney.Value.ToString("C");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label labKC = e.Row.FindControl("labKC") as Label;
                WLKCModel wl = e.Row.DataItem as WLKCModel;
                PageMoney += wl.KCZL * wl.PJDJ;
                labKC.Text = wl.KCZL.ToString("F");
                if (wl.KCZL < wl.ZDKCSL)
                    labKC.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            using (KCBLL kBLL = new KCBLL())
            {
                if (kBLL.Deletes("ID", id))
                    ShowMsg("删除成功");
                else
                    ShowMsg("删除失败");
            }
        }
    }
}